import axios from "../../http/axios";
import apis from "../../http/apis.js";
export default{
    namespaced: true,
    state:{
        pageData: {
            admins: [],

        },
        pages:0,
        total: 0,
        pageSize: 3,
        currentPage: 1,
        value:'',
        AdminschangeData: {

        }
    },
    mutations:{
        setAdmins(state,data) {
            state.pageData.admins = data.admins;
            state.pages=data.pages;
            state.total=data.total;
        },
        setPageSize(state,pageSize){
            state.pageSize=pageSize;    
        },
        setChangeData(state, data) {
            state.AdminschangeData = data
        },
        setCurrentPage(state,currentPage){
            if(currentPage>=1&&currentPage<=state.pages){
                state.currentPage=currentPage
            }
            
        }
    },
    actions:{
        async getAdmins(context,value) {
            const { data } = await axios({
                url:'/admins/getAdmins',
                method: 'get',
                params: {
                    pageSize:context.state.pageSize,
                    currentPage:context.state.currentPage,
                    value
                },
            });
            if (data.status) {
                context.commit('setAdmins', data.data);
                console.log("获取到了管理员数据",data.data);
                
            } else {

                alert('获取管理员数据失败')
            }
        },
        async deleteAdmins(context,adminsId) {
            const { data } = await apis.admins.deleteAdmins({
               adminsId
            });
            if (data.status) {
                alert('删除该管理员信息成功');
                context.dispatch('getAdmins')


            } else {
                alert('删除失败，请联系管理人员')
            }
        },
        async updateAdmins(context, adminsData) {
            
            const { data } = await apis.admins.updateAdmins({
                adminsData
            })
            if (data.status) {
                alert('修改管理员信息成功');
                context.dispatch('getAdmins')
            } else {
                alert('修改失败')
            }

        },
        async getAdminsById(context,_id) {
            const { data } = await apis.admins.getAdminsById({
                _id:_id.substring(1, 25)
            })
            if (data.status) {
                console.log('管理员数据',data.data[0]);
                context.commit('setChangeData',data.data[0]);
                
            } else {
                console.log(911, data);
            }
        },
        async addAdmins(context,admins) {
            const account = admins.account;
            const password = admins.password;
            const { data } = await axios({
                url: "/admins/addAdmins",
                method: 'post',
                data: {
                    password,account,
                }
            });
            if (data.status) {
                alert('新增管理员成功');

            } else {
                alert('新增失败')
            }
        },
    }
}